/**
 * 分页控制，得到总也数和判断是否有上下页
 * @author lixin
 */
package cn.edu.bit.lixin.blog.util;


public class PageBean {
	private int pageCount;//每页显示的记录数
	private int resultCount;//结果的总数
	private boolean hasPrevPage;//是否有上一页
	private boolean hasNextPage;//是否有下一页
	private int totalPage;//总页数
	private int currentPage;//当前的页码
	
	private String pageUrl;//分页前的页面地址

	public int getPageCount() {
		return pageCount;
	}

	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}

	public int getResultCount() {
		return resultCount;
	}

	public void setResultCount(int resultCount) {
		this.resultCount = resultCount;
	}

	public boolean isHasPrevPage() {
		return hasPrevPage;
	}

	public void setHasPrevPage(boolean hasPrevPage) {
		this.hasPrevPage = hasPrevPage;
	}

	public boolean isHasNextPage() {
		return hasNextPage;
	}

	public void setHasNextPage(boolean hasNextPage) {
		this.hasNextPage = hasNextPage;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public String getPageUrl() {
		return pageUrl;
	}

	public void setPageUrl(String pageUrl) {
		this.pageUrl = pageUrl;
	}
	
	/**
	 * 分页的计算，包括总页数，下一页和上一页
	 */
	public void caculate(){
		if (this.getPageCount() == 0){
			this.setPageCount(1);
		}
		
		totalPage = (int) Math.ceil(1.0 * getResultCount() / getPageCount()); // 总页面数
		if (totalPage == 0){
			totalPage = 1;
		}
		
		//check current page
		if (currentPage > totalPage){
			currentPage = totalPage;
		}
		
		hasNextPage = currentPage < totalPage;
        hasPrevPage = currentPage > 1;
	}
	
	
	 /**
     * 输出分页 HTML 页面跳转代码, 分链接和静态文字两种.
     * @return HTML 代码
     */
    public String getPageJumpLinkHtml() {
	    if(pageUrl.equals("") || pageUrl == null) {
	    	return "";
		}
	    
	    //检查current是否为0
	    if (currentPage == 0){
	    	currentPage = 1;
	    }
	    
	    // 检查是否有参数符号, 没有就加上一个?
	    if(pageUrl.indexOf('?') == -1) {
	    	pageUrl = pageUrl + '?';
	    }
	    
	    StringBuffer buff = new StringBuffer("");
	    
	    // 上一页翻页标记
	    if(currentPage > 1) {
	    	buff.append("[ <a href='" + pageUrl + "page=" + (currentPage - 1) + "'&title='转到第 "
					+ (currentPage - 1) + " 页'>上一页</a> ] ");
	    } else {
	    	buff.append("[ 上一页 ] ");
	    }
	    
	    // 下一页翻页标记
	    if(currentPage < getTotalPage()) {
	    	buff.append("<a href='" + pageUrl + "page=" + (currentPage + 1)+ "'&title='转到第 "
					+ (currentPage + 1) + " 页'>[ 下一页 ]</a>");
	    } else {
	    	buff.append("[ 下一页 ] ");
	    }
	    
	    System.out.println(buff.toString());
	    return buff.toString();
    }
}
